﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anke.SHManage.Utility
{
    /// <summary>
    /// 百度坐标系转换辅助类
    /// 从上海领导台程序中移植过来的
    /// </summary>
    public class CoorConvert
    {
        static double pi = 3.14159265358979324;

        static double a = 6378245.0;

        static double ee = 0.00669342162296594323;

        public readonly static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;


        public static double[] Wgs2bd(double lat, double lon)
        {

            double[] wgs2gcj = Wgs2gcj(lat, lon);

            double[] gcj2bd = Gcj2bd(wgs2gcj[0], wgs2gcj[1]);

            return gcj2bd;

        }


        public static double[] Gcj2bd(double lat, double lon)
        {

            double x = lon, y = lat;

            double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);

            double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);

            double bd_lon = z * Math.Cos(theta) + 0.0065;

            double bd_lat = z * Math.Sin(theta) + 0.006;

            return new double[] { bd_lat, bd_lon };

        }


        public static double[] Bd2gcj(double lat, double lon)
        {

            double x = lon - 0.0065, y = lat - 0.006;

            double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);

            double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);

            double gg_lon = z * Math.Cos(theta);

            double gg_lat = z * Math.Sin(theta);

            return new double[] { gg_lat, gg_lon };

        }


        public static double[] Wgs2gcj(double lat, double lon)
        {

            double dLat = TransformLat(lon - 105.0, lat - 35.0);

            double dLon = TransformLon(lon - 105.0, lat - 35.0);

            double radLat = lat / 180.0 * pi;

            double magic = Math.Sin(radLat);

            magic = 1 - ee * magic * magic;

            double sqrtMagic = Math.Sqrt(magic);

            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);

            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);

            double mgLat = lat + dLat;

            double mgLon = lon + dLon;

            double[] loc = { mgLat, mgLon };

            return loc;

        }


        private static double TransformLat(double lat, double lon)
        {

            double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.Sqrt(Math.Abs(lat));

            ret += (20.0 * Math.Sin(6.0 * lat * pi) + 20.0 * Math.Sin(2.0 * lat * pi)) * 2.0 / 3.0;

            ret += (20.0 * Math.Sin(lon * pi) + 40.0 * Math.Sin(lon / 3.0 * pi)) * 2.0 / 3.0;

            ret += (160.0 * Math.Sin(lon / 12.0 * pi) + 320 * Math.Sin(lon * pi / 30.0)) * 2.0 / 3.0;

            return ret;

        }


        private static double TransformLon(double lat, double lon)
        {

            double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.Sqrt(Math.Abs(lat));

            ret += (20.0 * Math.Sin(6.0 * lat * pi) + 20.0 * Math.Sin(2.0 * lat * pi)) * 2.0 / 3.0;

            ret += (20.0 * Math.Sin(lat * pi) + 40.0 * Math.Sin(lat / 3.0 * pi)) * 2.0 / 3.0;

            ret += (150.0 * Math.Sin(lat / 12.0 * pi) + 300.0 * Math.Sin(lat / 30.0 * pi)) * 2.0 / 3.0;

            return ret;

        }
    }
}
